package com.scpii.universal.struts.produce;

import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.hibernate.entity.AttributeTab;
import com.scpii.universal.hibernate.entity.CatelogTab;
import com.scpii.universal.service.CatelogService;
import com.scpii.universal.util.CatelogJsonBuilder;
import com.scpii.universal.util.FileUploadUtil;
import com.scpii.universal.util.ImgAndFilePath;

/**
 * universalWeb
 * @author liuhj
 * 2012-7-10下午10:51:06
 */
@Controller
@Scope("prototype")
public class CatelogProcessAction extends BaseAction {

	private static final long serialVersionUID = 4780979803960197037L;
	private List<CatelogTab> listCatelogs;
	private String json;
	private Integer id;
	private String imgURL;
	@Resource
	private CatelogService catelogService;
	private Log log = LogFactory.getLog(this.getClass());

	/**
	 * 保存分类及其属性
	 * @return
	 * @author liuhj
	 * 2012-7-10下午10:56:08
	 * @throws IOException 
	 */
	public String saveCatelog() throws IOException {
		json = URLDecoder.decode(json, "GBK");
		json = json.trim().substring(1, json.length()-1).replaceAll("\\\\", "");
		Integer catelogId = catelogService.save(json, getUserInfo(), getWebAppsRealPath(), getImgBasePath());
		this.setId(catelogId);
		return SUCCESS;
	}
	
	/**
	 * 获取直属下级分类
	 * @return
	 * @throws IOException
	 * @author liuhj
	 * 2012-8-1下午06:21:41
	 */
	public String listChildNextLevelCate() throws IOException {
		List<CatelogTab> catelogs = catelogService
				.getNextLevelChildren(JSONObject.fromObject(json).getInt("id"));
		if (catelogs != null && catelogs.size() > 0) {
			this.setListCatelogs(catelogs);
			return SUCCESS;
		}
		jsonOut(GenericConstants.OPERATION_NULL);
		return null;
	}
	
	/**
	 * 查询所有分类
	 * @return
	 * @author liuhj
	 * 2012-7-10下午10:58:09
	 * @throws IOException 
	 */
	public String listAllCatelog() throws IOException {
		CatelogJsonBuilder builder = new CatelogJsonBuilder();
		StringBuffer buf = new StringBuffer();
		if (getUserInfo().getAppId() == null || getUserInfo().getAppId() == 0) {
			jsonOut(GenericConstants.OPERATION_NULL);
			return null;
		}
		List<CatelogTab> catelogs = catelogService.getAll(getUserInfo());
		if (catelogs == null  || catelogs.size() <= 0) {
			jsonOut(GenericConstants.OPERATION_NULL);
			return null;
		}
		buf.append("{\"catelogTree\":").append(
				builder.buildJson(catelogs).replaceAll("\\[\\]", "\"-1\"")).append("}");
		jsonOut(buf.toString());
		return null;
	}
	
	/**
	 * 查询分类及属性信息
	 * @return
	 * @author liuhj
	 * 2012-8-3下午04:54:26
	 * @throws IOException 
	 */
	public String viewCatelogById() throws IOException {
		String[] properties = { "intCateId", "varCateName", "intParentCateId",
				"varCateImgUrl", "intHasSubCate", "varCateDescrip" ,"intType", "intPrivilege"};
		int cateId = JSONObject.fromObject(json).getInt("id");
		CatelogTab catelog = catelogService.getCatelogInfoById(cateId, properties);
		List<AttributeTab> attributes = catelogService.getAllAttributesByCateId(cateId);
		JSONObject datas = new JSONObject();
		datas.put("catelogInfo", catelog);
		datas.put("attributeInfo", attributes);
		response.getWriter().print(datas.toString());
		return null;
	}
	
	/**
	 * 根据分类ID查询所有子分类
	 * @return
	 * @author liuhj
	 * 2012-7-10下午10:58:09
	 */
	public String listChildCatelog() {
		this.setListCatelogs(catelogService.getAllChildrenTreeById(id));
		return SUCCESS;
	}
	
	/**
	 * 更新分类及属性
	 * @return
	 * @author liuhj
	 * 2012-7-10下午11:03:51
	 * @throws IOException 
	 */
	public String updateCatelog() throws IOException {
		json = URLDecoder.decode(json, "GBK");
		json = json.trim().substring(1, json.length()-1).replaceAll("\\\\", "");
		catelogService.update(json,getWebAppsRealPath(), getImgBasePath());
		jsonOut(GenericConstants.OPERATION_SUCCESS);
		return null;
	}
	
	/**
	 * 删除分类及子分类
	 * @return
	 * @author liuhj
	 * 2012-7-12下午09:08:15
	 * @throws IOException 
	 */
	public String deleteCatelog() throws IOException {
		catelogService.delete(JSONObject.fromObject(json).getInt("id"));
		jsonOut(GenericConstants.OPERATION_SUCCESS);
		return null;
	}
	
	/**
	 * 删除属性
	 * @author Taylor
	 * 2012-9-12 上午10:00:33
	 * @return
	 * String
	 * @throws IOException 
	 */
	public String deleteAttr() throws IOException {
		catelogService.deleteAttr(JSONObject.fromObject(json).getInt("id"));
		jsonOut(GenericConstants.OPERATION_SUCCESS);
		return null;
	}
	
	/**
	 * 分类缩略图上传
	 * @return
	 * @author liuhj
	 * 2012-7-15上午12:27:09
	 */
	public String catelogUrlUpload() {
		String fileName = (String) session.getAttribute(GenericConstants.TEMP_CATELOG_ICON_SESSION);
		String tempFolder = getWebAppsRealPath() + ImgAndFilePath.TEMP_FOLDER;
		File file = new File(tempFolder);
		if (!file.exists()) {
			file.mkdirs();
		}
		String imgDiskPath = getWebAppsRealPath() + ImgAndFilePath.TEMP_FOLDER+ File.separator + fileName;
		String imgURLPath = ImgAndFilePath.TEMP_FOLDER+ File.separator + fileName;
		FileUploadUtil.fileUpload(imgDiskPath, request);//上传
		//返回icon服务器上的URL
		this.setImgURL(getServerURL()+imgURLPath.replace("\\", "/"));
		log.info("已上传分类缩略图到临时文件");
		return SUCCESS;
	}

	public List<CatelogTab> getListCatelogs() {
		return listCatelogs;
	}

	public void setListCatelogs(List<CatelogTab> listCatelogs) {
		this.listCatelogs = listCatelogs;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getJson() {
		return json;
	}

	public void setJson(String json) {
		this.json = json;
	}

	public String getImgURL() {
		return imgURL;
	}

	public void setImgURL(String imgURL) {
		this.imgURL = imgURL;
	}
}
